#include "Mesh.h"
#include "IndexBuffer.h"
#include "VertexBufferManager.h"
#include "D3D9Renderer.h"

#include "../Utility/Renderer/GeometryLoader.h"
#include "../Utility/Renderer/GeometryGenerator.h"
#include "../Animation/AnimationManager.h"

Mesh::Mesh()
{
}

Mesh::~Mesh()
{
}

void Mesh::Load(string& _filePath, MESHTYPE _meshType)
{
	switch(_meshType)
	{
		case STATIC_MESH:
		{
			MeshDataPNT mesh;
			GeometryLoader<MeshDataPNT>::LoadStaticGeometry(_filePath.c_str(), mesh);

			vertCount			= mesh.vertices.size();
			startVertex		= VertexBufferManager::m_PosNormTexBuffer.AddVerts(&mesh.vertices[0], vertCount);
			startIndex		= IndexBuffer::AddIndices(&mesh.indices[0], mesh.indices.size());
			primitiveCount	= mesh.indices.size()/3;
		}
		break;
		
		case ANIMATED_MESH:
		{
			AnimationManager::LoadAnimatedMesh(_filePath, *this);
		}
		break;
	}
	
}

void Mesh::CreateBox(float _xScale, float _yScale, float _zScale)
{
	MeshDataPNT mesh;
	GeometryGenerator::CreateBoxPNT(_xScale, _yScale, _zScale, mesh);

	vertCount		= mesh.vertices.size();
	startVertex		= VertexBufferManager::m_PosNormTexBuffer.AddVerts(&mesh.vertices[0], vertCount);
	startIndex		= IndexBuffer::AddIndices(&mesh.indices[0], mesh.indices.size());
	primitiveCount	= mesh.indices.size()/3;
}

void Mesh::CreatePlane(float _xScale, float _yScale)
{
	MeshDataPT mesh;
	GeometryGenerator::CreatePlanePT(_xScale, _yScale, mesh);

	vertCount		= mesh.vertices.size();
	startVertex		= VertexBufferManager::m_PositionTexBuffer.AddVerts(&mesh.vertices[0], vertCount);
	startIndex		= IndexBuffer::AddIndices(&mesh.indices[0], mesh.indices.size());
	primitiveCount	= mesh.indices.size()/3;
}

void Mesh::RenderIndexedPrimitive()
{
	D3D9Renderer::device->SetStreamSource(0, VertexBufferManager::m_PosNormTexBuffer.GetVertexBuffer(), 0, sizeof(VertexPNT));
	D3D9Renderer::device->SetVertexDeclaration(VertexBufferManager::m_PosNormTexBuffer.GetVertexDeclaration());
	D3D9Renderer::device->SetIndices(IndexBuffer::m_pIndexBuff);
}